package com.itmayiedu.controller;

import com.itmayiedu.base.ResponseBase;
import com.itmayiedu.constants.Constants;
import com.itmayiedu.feign.PaySerivceFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;

/**
 * @Author cht 【wonderfor@vip.qq.com】
 * @Date 2018/11/2
 */
@RestController
@Slf4j
public class PayController {

    @Autowired
    PaySerivceFeign paySerivceFeign;

    //使用token进行支付
    @RequestMapping("/alipay")
    public void alipay(String payToken, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer =  response.getWriter();
        //1.验证参数
        if(StringUtils.isEmpty(payToken)) {
            return;
        }
        //2.调用支付宝接口,查找token是否存在,获取html元素
        ResponseBase responseBase = paySerivceFeign.findPayToken(payToken);
        if(!responseBase.getRtnCode().equals(Constants.HTTP_RES_CODE_200)) {
            String msg = responseBase.getMsg();
            writer.println(msg);
            return;
        }
        //3.获取结果
        LinkedHashMap data = (LinkedHashMap) responseBase.getData();
        String payHtml = (String) data.get("payHtml");
        log.info("######PayController######payHtml:{}", payHtml);
        //4.在页面上渲染
        writer.println(payHtml);
        writer.close();
    }
}
